package com.stylefeng.guns.rest.modular.film.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.api.film.service.FilmServiceApi;
import com.stylefeng.guns.api.film.vo.*;
import com.stylefeng.guns.core.util.DateUtil;
import com.stylefeng.guns.rest.common.persistence.dao.*;
import com.stylefeng.guns.rest.common.persistence.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @创建人 lIUJF
 * @创建时间 2019/1/8
 * @描述:
 */
@Component
@Service(interfaceClass = FilmServiceApi.class)
public class DefaultFilmServiceImpl implements FilmServiceApi {

	@Autowired
	private MoocBannerTMapper moocBannerTMapper;
	@Autowired
	private MoocFilmTMapper moocFilmTMapper;
	@Autowired
	private MoocCatDictTMapper moocCatDictTMapper;
	@Autowired
	private MoocYearDictTMapper moocYearDictTMapper;
	@Autowired
	private MoocSourceDictTMapper moocSourceDictTMapper;
	@Autowired
	private MoocFilmInfoTMapper moocFilmInfoTMapper;

	@Override
	public List<BannerVO> getBanners(){
		List<BannerVO> list = new ArrayList<>();
		List<MoocBannerT> moocBannerTS = moocBannerTMapper.selectList(null);
		for (MoocBannerT bannerT : moocBannerTS) {
			BannerVO vo = new BannerVO();
			vo.setBannerId(bannerT.getUuid() + "");
			vo.setBannerUrl(bannerT.getBannerUrl());
			vo.setBannerAddress(bannerT.getBannerAddress());
			list.add(vo);
		}
		return list;
	}

	@Override
	public FilmVO getHotFilms(boolean isLimit, int nums, int nowPage, int sortId, int sourceId, int yearId, int catId){
		FilmVO filmVO = new FilmVO();
		List<FilmInfo> filmInfos;

		// 热映影片的限制条件
		EntityWrapper<MoocFilmT> entityWrapper = new EntityWrapper<>();
		entityWrapper.eq("film_status", "1");
		// 判断是否是首页需要的内容
		if (isLimit) {
			// 如果是，则限制条数、限制内容为热映影片
			Page<MoocFilmT> page = new Page<>(1, nums);
			List<MoocFilmT> moocFilms = moocFilmTMapper.selectPage(page, entityWrapper);
			// 组织filmInfos
			filmInfos = getFilmInfos(moocFilms);
			filmVO.setFilmNum(moocFilms.size());
			filmVO.setFilmInfo(filmInfos);
		} else {
			// 如果不是，则是列表页，同样需要限制内容为热映影片
			Page<MoocFilmT> page = null;
			// 根据sortId的不同，来组织不同的Page对象
			// 1-按热门搜索，2-按时间搜索，3-按评价搜索
			switch (sortId) {
				case 1:
					page = new Page<>(nowPage, nums, "film_box_office");
					break;
				case 2:
					page = new Page<>(nowPage, nums, "film_time");
					break;
				case 3:
					page = new Page<>(nowPage, nums, "film_score");
					break;
				default:
					page = new Page<>(nowPage, nums, "film_box_office");
					break;
			}

			// 如果sourceId,yearId,catId 不为99 ,则表示要按照对应的编号进行查询
			if (sourceId != 99) {
				entityWrapper.eq("film_source", sourceId);
			}
			if (yearId != 99) {
				entityWrapper.eq("film_date", yearId);
			}
			if (catId != 99) {
				// #2#4#22#
				String catStr = "%#" + catId + "#%";
				entityWrapper.like("film_cats", catStr);
			}

			List<MoocFilmT> moocFilms = moocFilmTMapper.selectPage(page, entityWrapper);
			// 组织filmInfos
			filmInfos = getFilmInfos(moocFilms);
			filmVO.setFilmNum(moocFilms.size());

			// 需要总页数 totalCounts/nums -> 0 + 1 = 1
			// 每页10条，我现在有6条 -> 1
			int totalCounts = moocFilmTMapper.selectCount(entityWrapper);
			int totalPages = (totalCounts / nums) + 1;

			filmVO.setFilmInfo(filmInfos);
			filmVO.setTotalPage(totalPages);
			filmVO.setNowPage(nowPage);
		}

		return filmVO;
	}

	@Override
	public FilmVO getSoonFilms(boolean isLimit, int nums, int nowPage, int sortId, int sourceId, int yearId, int catId){
		FilmVO filmVO = new FilmVO();
		List<FilmInfo> filmInfos = new ArrayList<>();

		// 即将上映影片的限制条件
		EntityWrapper<MoocFilmT> entityWrapper = new EntityWrapper<>();
		entityWrapper.eq("film_status", "2");
		// 判断是否是首页需要的内容
		if (isLimit) {
			// 如果是，则限制条数、限制内容为热映影片
			Page<MoocFilmT> page = new Page<>(1, nums);
			List<MoocFilmT> moocFilms = moocFilmTMapper.selectPage(page, entityWrapper);
			// 组织filmInfos
			filmInfos = getFilmInfos(moocFilms);
			filmVO.setFilmNum(moocFilms.size());
			filmVO.setFilmInfo(filmInfos);
		} else {
			// 如果不是，则是列表页，同样需要限制内容为即将上映影片
			Page<MoocFilmT> page = null;
			// 根据sortId的不同，来组织不同的Page对象
			// 1-按热门搜索，2-按时间搜索，3-按评价搜索
			switch (sortId) {
				case 1:
					page = new Page<>(nowPage, nums, "film_preSaleNum");
					break;
				case 2:
					page = new Page<>(nowPage, nums, "film_time");
					break;
				case 3:
					page = new Page<>(nowPage, nums, "film_preSaleNum");
					break;
				default:
					page = new Page<>(nowPage, nums, "film_preSaleNum");
					break;
			}

			// 如果sourceId,yearId,catId 不为99 ,则表示要按照对应的编号进行查询
			if (sourceId != 99) {
				entityWrapper.eq("film_source", sourceId);
			}
			if (yearId != 99) {
				entityWrapper.eq("film_date", yearId);
			}
			if (catId != 99) {
				// #2#4#22#
				String catStr = "%#" + catId + "#%";
				entityWrapper.like("film_cats", catStr);
			}

			List<MoocFilmT> moocFilms = moocFilmTMapper.selectPage(page, entityWrapper);
			// 组织filmInfos
			filmInfos = getFilmInfos(moocFilms);
			filmVO.setFilmNum(moocFilms.size());

			// 需要总页数 totalCounts/nums -> 0 + 1 = 1
			// 每页10条，我现在有6条 -> 1
			int totalCounts = moocFilmTMapper.selectCount(entityWrapper);
			int totalPages = (totalCounts / nums) + 1;

			filmVO.setFilmInfo(filmInfos);
			filmVO.setTotalPage(totalPages);
			filmVO.setNowPage(nowPage);
		}

		return filmVO;
	}


	@Override
	public FilmVO getClassicFilms(int nums, int nowPage, int sortId, int sourceId, int yearId, int catId){
		FilmVO filmVO = new FilmVO();
		List<FilmInfo> filmInfos = new ArrayList<>();

		// 即将上映影片的限制条件
		EntityWrapper<MoocFilmT> entityWrapper = new EntityWrapper<>();
		entityWrapper.eq("film_status", "3");

		// 如果不是，则是列表页，同样需要限制内容为即将上映影片
		Page<MoocFilmT> page = null;
		// 根据sortId的不同，来组织不同的Page对象
		// 1-按热门搜索，2-按时间搜索，3-按评价搜索
		switch (sortId) {
			case 1:
				page = new Page<>(nowPage, nums, "film_box_office");
				break;
			case 2:
				page = new Page<>(nowPage, nums, "film_time");
				break;
			case 3:
				page = new Page<>(nowPage, nums, "film_score");
				break;
			default:
				page = new Page<>(nowPage, nums, "film_box_office");
				break;
		}

		// 如果sourceId,yearId,catId 不为99 ,则表示要按照对应的编号进行查询
		if (sourceId != 99) {
			entityWrapper.eq("film_source", sourceId);
		}
		if (yearId != 99) {
			entityWrapper.eq("film_date", yearId);
		}
		if (catId != 99) {
			// #2#4#22#
			String catStr = "%#" + catId + "#%";
			entityWrapper.like("film_cats", catStr);
		}

		List<MoocFilmT> moocFilms = moocFilmTMapper.selectPage(page, entityWrapper);
		// 组织filmInfos
		filmInfos = getFilmInfos(moocFilms);
		filmVO.setFilmNum(moocFilms.size());

		// 需要总页数 totalCounts/nums -> 0 + 1 = 1
		// 每页10条，我现在有6条 -> 1
		int totalCounts = moocFilmTMapper.selectCount(entityWrapper);
		int totalPages = (totalCounts / nums) + 1;

		filmVO.setFilmInfo(filmInfos);
		filmVO.setTotalPage(totalPages);
		filmVO.setNowPage(nowPage);

		return filmVO;
	}

	@Override
	public FilmDetailVO getFilmDetail(int searchType, String searchParam){
		FilmDetailVO filmDetailVO = null;
		// searchType 1-按名称  2-按ID的查找
		if (searchType == 1) {
			filmDetailVO = moocFilmTMapper.getFilmDetailByName("%" + searchParam + "%");
		} else {
			filmDetailVO = moocFilmTMapper.getFilmDetailById(searchParam);
		}

		return filmDetailVO;
	}


	@Override
	public List<FilmInfo> getBoxRanking(){
		//条件-》 正在上映的，票房前10名
		EntityWrapper<MoocFilmT> entityWrapper = new EntityWrapper<>();
		entityWrapper.eq("film_status", "1");
		Page<MoocFilmT> page = new Page<>(1, 10, "film_box_office");
		List<MoocFilmT> moocFilmTS = moocFilmTMapper.selectPage(page, entityWrapper);
		if (null != moocFilmTS && moocFilmTS.size() > 0) {
			List<FilmInfo> filmInfos = getFilmInfos(moocFilmTS);
			return filmInfos;
		}
		return null;
	}

	@Override
	public List<FilmInfo> getExpectRanking(){
		//条件-》即将上映的预售前10名
		EntityWrapper<MoocFilmT> entityWrapper = new EntityWrapper<>();
		entityWrapper.eq("film_status", "2");
		Page<MoocFilmT> page = new Page<>(1, 10, "film_preSaleNum");
		List<MoocFilmT> moocFilmTS = moocFilmTMapper.selectPage(page, entityWrapper);
		if (null != moocFilmTS && moocFilmTS.size() > 0) {
			List<FilmInfo> filmInfos = getFilmInfos(moocFilmTS);
			return filmInfos;
		}
		return null;
	}

	@Override
	public List<FilmInfo> getTop(){
		//条件-》 正在上映，评分前10名
		EntityWrapper<MoocFilmT> entityWrapper = new EntityWrapper<>();
		entityWrapper.eq("film_status", "2");
		Page<MoocFilmT> page = new Page<>(1, 10, "film_score");
		List<MoocFilmT> moocFilmTS = moocFilmTMapper.selectPage(page, entityWrapper);
		if (null != moocFilmTS && moocFilmTS.size() > 0) {
			List<FilmInfo> filmInfos = getFilmInfos(moocFilmTS);
			return filmInfos;
		}
		return null;
	}

	@Override
	public List<CatVO> getCats(){
		List<CatVO> cats = new ArrayList<>();
		// 查询实体对象 - MoocCatDictT
		List<MoocCatDictT> moocCats = moocCatDictTMapper.selectList(null);
		// 将实体对象转换为业务对象 - CatVO
		for (MoocCatDictT moocCatDictT : moocCats) {
			CatVO catVO = new CatVO();
			catVO.setCatId(moocCatDictT.getUuid() + "");
			catVO.setCatName(moocCatDictT.getShowName());
			cats.add(catVO);
		}
		return cats;
	}

	@Override
	public List<SourceVO> getSources(){
		List<SourceVO> sources = new ArrayList<>();
		List<MoocSourceDictT> moocSourceDicts = moocSourceDictTMapper.selectList(null);
		for (MoocSourceDictT moocSourceDictT : moocSourceDicts) {
			SourceVO sourceVO = new SourceVO();
			sourceVO.setSourceId(moocSourceDictT.getUuid() + "");
			sourceVO.setSourceName(moocSourceDictT.getShowName());
			sources.add(sourceVO);
		}
		return sources;
	}

	@Override
	public List<YearVO> getYears(){
		List<YearVO> years = new ArrayList<>();
		// 查询实体对象 - MoocCatDictT
		List<MoocYearDictT> moocYears = moocYearDictTMapper.selectList(null);
		// 将实体对象转换为业务对象 - CatVO
		for (MoocYearDictT moocYearDictT : moocYears) {
			YearVO yearVO = new YearVO();
			yearVO.setYearId(moocYearDictT.getUuid() + "");
			yearVO.setYearName(moocYearDictT.getShowName());
			years.add(yearVO);
		}
		return years;
	}

	private List<FilmInfo> getFilmInfos(List<MoocFilmT> filmTS){
		List<FilmInfo> infos = new ArrayList<>();
		for (MoocFilmT filmT : filmTS) {
			FilmInfo filmInfo = new FilmInfo();
			filmInfo.setBoxNum(filmT.getFilmPresalenum());
			filmInfo.setShowTime(DateUtil.getDay(filmT.getFilmTime()));
			filmInfo.setScore(filmT.getFilmScore());
			filmInfo.setImgAddress(filmT.getImgAddress());
			filmInfo.setFilmType(filmT.getFilmType());
			filmInfo.setFilmScore(filmT.getFilmScore());
			filmInfo.setFilmName(filmT.getFilmName());
			filmInfo.setFilmId(filmT.getUuid() + "");
			filmInfo.setExpectNum(filmT.getFilmPresalenum());
			infos.add(filmInfo);
		}
		return infos;
	}

}
